# -*- coding:utf-8 -*-
"""
作者：Wangzhicheng
日期：2024年08月04日13:27
@file 去哪儿网攻略库.py
"""
import time

from bs4 import BeautifulSoup
from download import get_text
import pymysql

db = pymysql.connect(host="127.0.0.1",
                     port=3306,
                     user="root",
                     password="123456",
                     db="travelduct"
                     )
# 创建游标（执行者）
cursor = db.cursor()
# 如果scenic_spot表已经存在，可以先删除
# sql = "DROP TABLE IF EXISTS scenic_spot;"
# cursor.execute(sql)

# sql = """CREATE TABLE scenic_spot(
#     title VARCHAR(166),
#     author CHAR(166),
#     date VARCHAR(20),
#     days CHAR(20),
#     photo_nums CHAR(30),
#     per_capita_fee VARCHAR(20),
#     people CHAR(88),
#     trip VARCHAR(188),
#     road VARCHAR(188),
#     schedule VARCHAR(199),
#     img1 VARCHAR(166),
#     img2 VARCHAR(166),
#     img3 VARCHAR(166),
#     img4 VARCHAR(166)
#     );
#
#     """
# cursor.execute(sql)

def get_introduction():
    travel_data = []
    for i in range(200):
        url = f"https://travel.qunar.com/travelbook/list.htm?page={i+1}&order=hot_heat"
        text = get_text(url)
        # print(text)
        bs =BeautifulSoup(text,"lxml")
        data = bs.find("ul",attrs={"class":"b_strategy_list"})
        # print(data)
        data_list = data.find_all("li",{"class":"list_item"})
        # print(len(data_list))
        for guide in data_list:
            road = ""
            schedule = ""
            per_capita_fee = "人均元"
            poeple = ""
            img2 = ""
            img3 = ""
            img4 = ""

            # print(guide)
            title = guide.find("h2",{"class":"tit"}).find("a").text
            # print(title)
            author = guide.find("span",{"class":"user_name"}).find("a").text
            # print(author)
            date =  guide.find("span",{"class":"date"}).text
            # print(date)
            days = guide.find("span",{"class":"days"}).text
            # print(days)
            photo_nums = guide.find("span",{"class":"photo_nums"}).text
            img1 = guide.find("ul", {"class": "pics"}).find_all("li")[0].find("img")["src"]
            try:
                trip = guide.find("span", {"class": "trip"}).text
                people = guide.find("span", {"class": "people"}).text
                per_capita_fee = guide.find("span", {"class": "fee"}).text
                road = guide.find_all("p",{"class":"places"})[0].text
                schedule = guide.find_all("p",{"class":"places"})[1].text
                img2 = guide.find("ul", {"class": "pics"}).find_all("li")[1].find("img")["src"]
                img3 = guide.find("ul", {"class": "pics"}).find_all("li")[2].find("img")["src"]
                img4 = guide.find("ul", {"class": "pics"}).find_all("li")[3].find("img")["src"]
            except Exception as ex:
                pass
            travel_data.append([title,author,date,days,photo_nums,per_capita_fee,people,trip,road,schedule,img1,img2,img3,img4])
    return travel_data

def main():
   data = get_introduction()
   sql = "INSERT INTO scenic_spot VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
   cursor.executemany(sql, data)
   db.commit()
   db.close()


if __name__ == "__main__":
    main()
